#include "widget.h"
#include "ui_widget.h"
#include "date.h"
#include "recv.h"
#include <qtabbar.h>
#include <QDebug>
#include <QTimer>
#include <QRegularExpression>
#include <QPushButton>
#include <QtWidgets>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    hideTimer(new QTimer(this)) // Initialize hideTimer in the constructor
{
    qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
    ui->setupUi(this);



    for (int floor = 1; floor <= 3; ++floor)
    {
        for (char section = 'A'; section <= 'D'; ++section)
        {
            for (int position = 1; position <= 10; ++position)
            {
                // 使用QString的arg方法来格式化spotId
                QString spotId = QString("%1%2%3").arg(section).arg(floor).arg(position, 2, 10, QChar('0'));
                ParkingDate* spot = new ParkingDate(spotId);
                m_parkingSpots.append(spot);
            }
        }
    }
    //setupConnections();
    // 遍历m_parkingSpots数组并使用qDebug()输出每个ParkingDate对象的信息
    for (const auto& spot : m_parkingSpots)
    {
        qDebug() << "Parking Spot ID:" << spot->getSpotId()
                 << "Occupied:" << spot->isOccupied()
                 << "No:" << spot->getNo();
    }

    for (const auto& spot : m_parkingSpots) {
        updateButtonColorForSpot(spot);
        updateWidgetColorForSpot(spot);
    }
    ui->B2widget->hide();
    ui->B3widget->hide();
    navigationMarkerHiding();
    // Connect the timer's timeout signal to a lambda function that hides the widgets
    connect(hideTimer, &QTimer::timeout, this, [this]() {
        enableButton();
        navigationMarkerHiding();
    });
}

Widget::~Widget()
{
    delete ui;
    delete hideTimer;
}

void Widget::navigationMarkerHiding()
{
    ui->up2->hide();
    ui->right2->hide();
    ui->up1->hide();
    ui->right1->hide();
    ui->down2->hide();
    ui->down1->hide();
    ui->A101->hide();
    ui->A102->hide();
    ui->A103->hide();
    ui->A104->hide();
    ui->A105->hide();
    ui->A106->hide();
    ui->A107->hide();
    ui->A108->hide();
    ui->A109->hide();
    ui->A110->hide();
    ui->B101->hide();
    ui->B102->hide();
    ui->B103->hide();
    ui->B104->hide();
    ui->B105->hide();
    ui->B106->hide();
    ui->B107->hide();
    ui->B108->hide();
    ui->B109->hide();
    ui->B110->hide();
    ui->C101->hide();
    ui->C102->hide();
    ui->C103->hide();
    ui->C104->hide();
    ui->C105->hide();
    ui->C106->hide();
    ui->C107->hide();
    ui->C108->hide();
    ui->C109->hide();
    ui->C110->hide();
    ui->D101->hide();
    ui->D102->hide();
    ui->D103->hide();
    ui->D104->hide();
    ui->D105->hide();
    ui->D106->hide();
    ui->D107->hide();
    ui->D108->hide();
    ui->D109->hide();
    ui->D110->hide();
    ui->A201->hide();
    ui->A202->hide();
    ui->A203->hide();
    ui->A204->hide();
    ui->A205->hide();
    ui->A206->hide();
    ui->A207->hide();
    ui->A208->hide();
    ui->A209->hide();
    ui->A210->hide();
    ui->B201->hide();
    ui->B202->hide();
    ui->B203->hide();
    ui->B204->hide();
    ui->B205->hide();
    ui->B206->hide();
    ui->B207->hide();
    ui->B208->hide();
    ui->B209->hide();
    ui->B210->hide();
    ui->C201->hide();
    ui->C202->hide();
    ui->C203->hide();
    ui->C204->hide();
    ui->C205->hide();
    ui->C206->hide();
    ui->C207->hide();
    ui->C208->hide();
    ui->C209->hide();
    ui->C210->hide();
    ui->D201->hide();
    ui->D202->hide();
    ui->D203->hide();
    ui->D204->hide();
    ui->D205->hide();
    ui->D206->hide();
    ui->D207->hide();
    ui->D208->hide();
    ui->D209->hide();
    ui->D210->hide();
    ui->A301->hide();
    ui->A302->hide();
    ui->A303->hide();
    ui->A304->hide();
    ui->A305->hide();
    ui->A306->hide();
    ui->A307->hide();
    ui->A308->hide();
    ui->A309->hide();
    ui->A310->hide();
    ui->B301->hide();
    ui->B302->hide();
    ui->B303->hide();
    ui->B304->hide();
    ui->B305->hide();
    ui->B306->hide();
    ui->B307->hide();
    ui->B308->hide();
    ui->B309->hide();
    ui->B310->hide();
    ui->C301->hide();
    ui->C302->hide();
    ui->C303->hide();
    ui->C304->hide();
    ui->C305->hide();
    ui->C306->hide();
    ui->C307->hide();
    ui->C308->hide();
    ui->C309->hide();
    ui->C310->hide();
    ui->D301->hide();
    ui->D302->hide();
    ui->D303->hide();
    ui->D304->hide();
    ui->D305->hide();
    ui->D306->hide();
    ui->D307->hide();
    ui->D308->hide();
    ui->D309->hide();
    ui->D310->hide();
    ui->up1_2->hide();
    ui->up2_2->hide();
    ui->up1_3->hide();
    ui->up2_3->hide();
    ui->down1_2->hide();
    ui->down2_2->hide();
    ui->down1_3->hide();
    ui->down2_3->hide();
    ui->right1_2->hide();
    ui->right2_2->hide();
    ui->right3_2->hide();
    ui->right1_3->hide();
    ui->right2_3->hide();
    ui->right3_3->hide();
    ui->right3->hide();
}


void Widget::on_b1Button_clicked()
{
    // 修改b1Button的背景颜色并禁用它
    QString origStyle = ui->b1Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #27639F;");
    ui->b1Button->setStyleSheet(origStyle);
    ui->b1Button->setEnabled(false);

    // 修改b2Button和b3Button的背景颜色
    origStyle = ui->b2Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #566772;");
    ui->b2Button->setStyleSheet(origStyle);
    ui->b2Button->setEnabled(true);
    origStyle = ui->b3Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #566772;");
    ui->b3Button->setStyleSheet(origStyle);
    ui->b3Button->setEnabled(true);

    ui->B1widget->show();
    ui->B2widget->hide();
    ui->B3widget->hide();
}



void Widget::on_b2Button_clicked()
{
    // 修改b1Button的背景颜色并禁用它
    QString origStyle = ui->b2Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #27639F;");
    ui->b2Button->setStyleSheet(origStyle);
    ui->b2Button->setEnabled(false);

    // 修改b2Button和b3Button的背景颜色
    origStyle = ui->b1Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #566772;");
    ui->b1Button->setStyleSheet(origStyle);
    ui->b1Button->setEnabled(true);

    origStyle = ui->b3Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #566772;");
    ui->b3Button->setStyleSheet(origStyle);
    ui->b3Button->setEnabled(true);

    ui->B1widget->hide();
    ui->B2widget->show();
    ui->B3widget->hide();
}

void Widget::on_b3Button_clicked()
{
    // 修改b3Button的背景颜色并禁用它
    QString origStyle = ui->b3Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #27639F;");
    ui->b3Button->setStyleSheet(origStyle);
    ui->b3Button->setEnabled(false);

    // 修改b1Button和b2Button的背景颜色
    origStyle = ui->b1Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #566772;");
    ui->b1Button->setStyleSheet(origStyle);
    ui->b1Button->setEnabled(true);

    origStyle = ui->b2Button->styleSheet();
    origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #566772;");
    ui->b2Button->setStyleSheet(origStyle);
    ui->b2Button->setEnabled(true);

    ui->B1widget->hide();
    ui->B2widget->hide();
    ui->B3widget->show();
}

void Widget::updateButtonColorForSpot(ParkingDate* spot) {
    QString buttonName = "pushButton" + spot->getSpotId();
    QPushButton *button = findChild<QPushButton*>(buttonName);
    if (button != nullptr)
    {
        if (spot->isOccupied())
        {
            QString origStyle = button->styleSheet();
            origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: rgb(174, 52, 50);");
            button->setStyleSheet(origStyle);
            button->setEnabled(false);
        }
        else
        {
            QString origStyle = button->styleSheet();
            origStyle = origStyle.replace(QRegularExpression("background-color:.*;"), "background-color: #00CDA5;");
            button->setStyleSheet(origStyle);
            button->setEnabled(true);
        }
    }
    else
    {

    }
}

void Widget::updateWidgetColorForSpot(ParkingDate* spot)
{
    QString widgetName = spot->getSpotId() + "widget";
    QWidget *widget = findChild<QWidget*>(widgetName);
    if (widget != nullptr)
    {
        // 保存当前样式表，以便在需要时恢复它
        QString oldStyleSheet = widget->styleSheet();

        if (spot->isOccupied())
        {
            widget->setStyleSheet("QWidget { background-color: rgb(174, 52, 50); }");
        }
        else
        {
            widget->setStyleSheet("QWidget { background-color: #00CDA5; }");
        }

        // 强制更新样式表
        widget->style()->unpolish(widget);
        widget->style()->polish(widget);
        widget->update();
    }
    else
    {

    }
}
